<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" style="margin-top: 15px">
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 60px">用户姓名:</label>
        <div class="layui-input-inline">
            <input type="text" name="nickName" id="nickName" autocomplete="off"
                   class="layui-input">
        </div>
        <label class="layui-form-label" style="width: 60px">用户账号:</label>
        <div class="layui-input-inline">
            <input type="text" name="account" id="account" autocomplete="off"
                   class="layui-input">
        </div>
        <label class="layui-form-label" style="width: 60px">兑换码:</label>
        <div class="layui-input-inline">
            <input type="text" name="code" id="code" autocomplete="off"
                   class="layui-input">
        </div>
        <label class="layui-form-label" style="width: 60px;margin-left: 5px">会员等级:</label>
        <div class="layui-input-inline" style="width: 6%">
            <select name="vipLevel" id="vipLevel">
                <option value="">全部</option>
                <option value="1">试用会员</option>
                <option value="2">任享会员</option>
                <option value="3">尊享会员</option>
                <option value="4">季卡会员</option>
            </select>
        </div>
        <label class="layui-form-label" style="width: 60px;margin-left: 5px">状态:</label>
        <div class="layui-input-inline" style="width: 6%">
            <select name="status" id="status">
                <option value="1">已使用</option>
            </select>
        </div>
        <button class="layui-btn" id="searchBtn" lay-submit lay-filter="searchBtn">查询</button>
        <a id="down" class="layui-btn layui-btn-radius" download="兑换码信息.csv" href="#">导出</a>
    </div>
</form>
<table class="layui-table" id="grid" lay-filter="grid">
</table>
<script th:replace="/common/inc::incJs"></script>
<script>
    var tableIns;
    var allStr;

    $(function () {
        tableIns = createTable({
            url: '/backend/security/bdTeamCodeLog/codeList',
            cols: [[
                {field: 'bdTeamName', title: '所属代理商', width: 180}
                , {
                    field: 'account',
                    title: '用户手机',
                    width: 180
                }, {
                    field: 'nickName',
                    title: '用户昵称',
                    width: 160
                }, {
                    field: 'vipLevel',
                    title: '状态',
                    width: 120, templet: '#vipTpl'
                }, {
                    field: 'status',
                    title: '状态',
                    width: 80, templet: '#statusTpl'
                }, {
                    field: 'modifyTime',
                    title: '兑换时间',
                    width: 160
                }, {
                    field: 'code',
                    title: '兑换码',
                    width: 160
                }
            ]]
        })

        $.post('/backend/security/bdTeamCodeLog/codeList', function (res) {
            if (res.code < 0) {
                layer.msg(res.message);
            }
            else {
                allStr = res.body;
            }
        });
    });

    $('#down').on('click', function (aLink) {
        var table = layui.table;
        var tab = allStr;
        var str = "所属代理商,用户手机,用户昵称,兑换时间\n";
        var vipStr="";
        $.each(tab, function (i, row) {
            if (row["status"] == "0") {
                vipStr = "待支付";
            } else if (row["status"] == "1") {
                vipStr = "待入住";
            } else if (row["status"] == "2") {
                vipStr = "已入住";
            } else if (row["status"] == "3") {
                vipStr = "已完成";
            }
            str = str + row["bdTeamName"] + "," + row["account"] + "," + row["nickName"] + "," + row["modifyTime"] + "\n";
        });
        str = encodeURIComponent(str);
        aLink.target.href = "data:text/csv;charset:utf-8,\ufeff" + str;
    });


    layui.table.on('tool(grid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象02\
        if (layEvent === 'change') { //删除
            changeStatus(data.id, data.delBoolean);
        } else if (layEvent === 'edit') { //编辑
            openDialog({
                url: "/backend/security/sysDictionary/detail?id=" + data.id
            });
        }
    });

    //变更标签状态
    function changeStatus(id, status) {
        if (status == "0") {
            $.post('/backend/security/sysDictionary/update', {id: id, delBoolean: 1}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("禁用成功");
                        tableIns.reload();
                    }
                }
            )
        } else if (status = "1") {
            $.post('/backend/security/sysDictionary/update', {id: id, delBoolean: 0}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("启用成功");
                        tableIns.reload();
                    }
                }
            )
        }
    }

    layui.form.on('submit(addBtn)', function () {
        openDialog({
            url: "/backend/security/sysDictionary/detail",
            btn: ["保存", "取消"]
        });
        return false;
    });

    layui.form.on('submit(searchBtn)', function (data) {
        tableIns.reload({
            where: data.field
        });

        $.post('/backend/security/bdTeamCodeLog/codeList',{
            nickName:$("#nickName").val(),
            account:$("#account").val(),
            code:$("#code").val(),
            status:$("#status").val(),
            vipLevel:$("#vipLevel").val()
        }, function (res) {
            if (res.code < 0) {
                layer.msg(res.message);
            }
            else {
                allStr = res.body;
            }
        });
        return false;
    });
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status ===1){ }}
    <span style="color: #FF0000;">已使用</span>
    {{#  } else { }}
    <span style="color: green;">未使用</span>
    {{#  } }}
</script>

<script type="text/html" id="vipTpl">
    {{#  if(d.vipLevel ===1){ }}
    <span>试用会员</span>
    {{#  } else if(d.vipLevel ===2){ }}
    <span>任享会员</span>
    {{#  } else if(d.vipLevel ===3){ }}
    <span>尊享会员</span>
    {{#  } else if(d.vipLevel ===4){ }}
    <span>季卡会员</span>
    {{#  } }}
</script>
</body>
</html>
